<template>
  <div class="custom-quality-engineering-container page-common-layout">
    <div class="screen-conditions-container">
      <a-select
        showSearch
        :filter-option="filterOption"
        placeholder="请选择专业"
        v-model="condition.professionType"
        :loading="condition.professionLoading"
      >
        <a-select-option
          :value="item.dicItemCode"
          v-for="item in condition.professionSource"
          :key="item.uuid"
        >
          {{ item.dicItemName }}
        </a-select-option>
      </a-select>
      <div class="group-button">
        <a-button class="cu-btn-primary" icon="search" @click="search()">
          查询
        </a-button>
        <a-button class="cu-btn-filter" icon="reload" @click="reset()">
          重置
        </a-button>
      </div>
    </div>
    <div class="table-container">
      <div class="header" header="专家库">
        <a-button
          @click="gotoAdd()"
          class="cu-btn-primary margin-bottom-xs"
          icon="plus"
          v-action:zjk_add
        >
          新增
        </a-button>
      </div>
      <div class="main">
        <vxe-table
          ref="xTable"
          height="99%"
          align="center"
          show-overflow
          show-header-overflow
          border="inner"
          :data="tableData"
          :column-config="{ resizable: true }"
          size="small"
          :loading="loading"
        >
          <vxe-column type="seq" width="80" title="序号"></vxe-column>
          <vxe-column field="expertName" title="专家姓名"> </vxe-column>
          <vxe-column field="idCard" title="身份证号"> </vxe-column>
          <vxe-column field="mobile" title="联系方式/登录账号" width="160">
          </vxe-column>
          <vxe-column field="professionName" title="专业"> </vxe-column>
          <vxe-column field="technicalTitleName" title="职称"> </vxe-column>
          <vxe-column field="sexName" title="性别"> </vxe-column>
          <vxe-column field="nativePlace" title="籍贯"> </vxe-column>
          <vxe-column field="birthDate" title="出生年月"> </vxe-column>
          <vxe-column field="enterTime" title="入库时间"> </vxe-column>
          <vxe-column
            field="operate"
            width="220"
            title="操作"
            fixed="right"
            align="left"
          >
            <template #default="{ row }">
              <div class="text-button-group">
                <a-button type="link" @click="gotoEdit(row)" v-action:zjk_edit>
                  编辑
                </a-button>
                <a-button
                  type="link"
                  @click="handleDelClick(row)"
                  class="danger"
                  v-action:zjk_delete
                >
                  删除
                </a-button>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="pager-container">
        <vxe-pager
          size="small"
          background
          :current-page.sync="pager.page"
          :page-size.sync="pager.pageSize"
          :total="pager.total"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes']"
          :page-sizes="[20, 50, 100, 500]"
          @page-change="paginationChange"
        >
          <template #left>
            <div class="vxe-pager-left-tip-container">
              共<span>{{ pager.total }}</span
              >条
            </div>
          </template>
        </vxe-pager>
      </div>
    </div>
    <!-- 新增弹窗 -->
    <addModal
      :visible.sync="addModalVisible"
      :parameter="dataSoucre"
      :key="modalKey"
    ></addModal>
  </div>
</template>
<script>
import { axios } from "@/utils/request";
import addModal from "./components/addModal";
export default {
  components: {
    addModal,
  },
  data() {
    return {
      loading: false,
      tableData: [],
      pager: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      //新增弹窗
      addModalVisible: false,
      dataSoucre: {
        text: "",
        id: "",
      },
      modalKey: 0,
      condition: {
        //专家姓名
        expertName: "",
        //专业
        professionType: undefined,
        professionSource: [],
        professionLoading: false,
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      Promise.all([this.getMajorList(), this.getTableData()])
        .then()
        .finally((_) => {});
    },
    search() {
      this.pager.page = 1;
      this.getTableData();
    },

    reset() {
      this.condition.expertName = "";
      this.condition.professionType = undefined;
      this.search();
    },

    //查询专业
    getMajorList() {
      this.condition.professionLoading = true;
      return new Promise((resolve, reject) => {
        this.$post("/system/dicSet/getDicItemByCode", {
          code: "professionType",
        })
          .then((res) => {
            let { data } = res;
            this.condition.professionSource = data || [];
            resolve();
          })
          .catch((_) => {
            this.condition.professionSource = [];
            reject();
          })
          .finally((_) => {
            this.condition.professionLoading = false;
          });
      });
    },
    //select 搜索过滤
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
    //新增
    gotoAdd() {
      this.modalKey = Date.now();
      this.dataSoucre = {
        text: "新增",
      };
      this.addModalVisible = true;
    },
    //编辑
    gotoEdit(row) {
      this.modalKey = Date.now();
      this.dataSoucre = {
        text: "编辑",
        ...row,
      };
      this.addModalVisible = true;
    },
    //删除
    handleDelClick(row) {
      let dataLength = this.tableData.length;
      let that = this;
      that.$confirm({
        title: "提示",
        content: "确定删除该数据吗？删除之后不可恢复！",
        okText: "确定",
        cancelText: "取消",
        icon: () =>
          that.$createElement("a-icon", {
            props: {
              key: "warning",
              type: "exclamation-circle",
              twoToneColor: "#faad14",
            },
          }),
        onOk() {
          that
            .$post(`/probusiness/configExpert/deleteBatch`, {
              varchar: row.uuid,
            })
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                that.$message.success("删除成功", 1.5);
                if (dataLength == 1 && that.pager.page > 1) {
                  that.pager.page -= 1;
                }
                that.getTableData();
              } else {
                that.$message.error(res.message);
              }
            })
            .catch((_) => {});
        },
      });
    },

    //查询列表数据
    getTableData() {
      this.loading = true;
      let { page, pageSize } = this.pager;
      let { expertName, professionType = "" } = this.condition;
      axios
        .post(
          `/probusiness/configExpert/getPageSet?pageNo=${page}&pageSize=${pageSize}`,
          {
            professionType,
            expertName,
          },
          {
            headers: {
              "Content-Type": "application/json",
            },
          }
        )
        .then((res) => {
          this.tableData = res?.rows ?? [];
          this.pager.total = res?.totalCount ?? 0;
        })
        .catch((_) => {
          this.tableData = [];
          this.pager.total = 0;
        })
        .finally((_) => {
          this.loading = false;
        });
    },
    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getTableData();
    },
  },
};
</script>
<style lang="less" scoped></style>
